/**
 * Created by teddy on 6/30/16.
 */

// 取URL参数
function getUrlParam(name) {
    //构造一个含有目标参数的正则表达式对象
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    //匹配目标参数
    var r = window.location.search.substr(1).match(reg);
    //返回参数值
    if (r != null) return unescape(r[2]); return null;
}

var columnOptions = {
    chart: {
        renderTo: 'columnChart',
        type: 'bar'
    },
    colors: ['#50B432','#ED561B'],
    title: {
        text: 'Case Success Rate'
    },

    xAxis: {
        categories: [],
        labels: {
            style: {
                color: "#000000",
                fontSize: '14px',
                fontWeight: '600',
                fontFamily: ''
            }
        }
    },

    yAxis: {
        allowDecimals: true,
        min: 0,
        title: {
            text: ''
        }
    },
    tooltip: {
        formatter: function () {
            var s = '<b style="font-size:14px;font-weight:800">' + this.x + '</b>';
            $.each(this.points, function (i,point) {
                /*For Debug*/
                /*console.log(point);*/
                s += '<br/>' + '<span style="font-size:14px;color:' +point.color + '">' + point.series.name + '</span>: <b>' + point.y + '</b> (' + parseFloat(point.percentage).toFixed(2) + '%)<br/>';
            });

            return s;
        },
        shared: true
    },
    plotOptions: {
        series: {
            stacking: 'percent'
        }
    },
    credits: {
        enabled: false
    },
    series: [{
        name: 'Success',
        stack: 'male'
    }, {
        name: 'Fail',
        stack: 'male'
    }]
}

// Go
$(function () {

    var jobID = getUrlParam('jobid');
    var status = getUrlParam('status');
    if (status == 1) {
        $('.info-box').addClass("bg-green");
    }
    else {
        $('.info-box').addClass("bg-red");
    }

    $('#JobIDLabel').html(jobID);
    $('#CaseTable').bootstrapTable({
        url: 'getAppUITestCaseByJobID.php?jobid=' + jobID,
        onLoadSuccess: function() {
            $('.imageview').miniPreview({ prefetch: 'none' });
        }
    });

    $('.imageview').miniPreview({ prefetch: 'none' });

    // Chart
    var xName = [];
    var successData = [];
    var failData = [];

    $.ajax({
        type: "GET",
        url: "getColumnAppUIData.php?jobid=" + jobID,
        success: function(tsv) {
            tsv = tsv.split(/\n/g);
            jQuery.each(tsv, function(i, line) {
                line = line.split(/\t/);

                if(line[0] != "") {
                    if ($.inArray(line[0], xName) < 0) {
                        xName.push(line[0]);
                    }
                }
            });

            jQuery.each(xName, function(i, name) {

                var successNumber = 0;
                var failNumber = 0;

                jQuery.each(tsv, function (x, line){

                    line = line.split(/\t/);

                    if(name == line[0]) {

                        if (line[1] == "Success") {

                            successNumber = parseInt(line[2],10);

                        }
                        else {
                            failNumber = parseInt(line[2],10);
                        }
                    }
                });
                failData.push(failNumber);
                successData.push(successNumber);

            });

            columnOptions.xAxis.categories = xName;
            columnOptions.series[0].data = successData;
            columnOptions.series[1].data = failData;
            var columnChart = new Highcharts.Chart(columnOptions);
        }
    })

});

function snapshotFormat(index, row) {
    return '<a target="_blank" style="margin-left: -25px;" class="imageview" href="' + row["snapshot"] + '">Preview</a>';
}

function rowStyle(row, index) {

    var classes = ['active', 'success', 'info', 'warning', 'danger'];

    if (row["result"] == "Success") {
        return {
            classes: classes[1]
        };
    }
    else {
        return {
            classes: classes[4]
        };
    }

    return {};
}
